package com.example.sunmiv1.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;


import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.AllocationTubeListAlloDetailsAdapter;
import com.example.sunmiv1.adapter.AllocationTubeListWareHDetailsAdapter;
import com.example.sunmiv1.base.BaseActivity;
import com.example.sunmiv1.entity.AllocationTubeBean;
import com.example.sunmiv1.entity.AllocationTubeListAlloDetailsBean;
import com.example.sunmiv1.entity.AllocationTubeListWareHDetailsBean;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.GsonUtils;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AllocationTubeListDetailsActivity extends BaseActivity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.rv)
    RecyclerView rv;

    private static final String ALLOCATION_DETAILS="0";
    private static final String WAREHOUSE_DETAILS="1";
    private static final int GET_ALLOCATION_DETAILS_INFO=0x000a;
    private static final int GET_WAREHOUSE_DETAILS_INFO=0x000b;

    private String acticityType;
    private String orderId; //  訂單ID
    private String warehouseRecordId;//  入庫記錄ID

    private LoadingDialog loadingDialog;

    private AllocationTubeListWareHDetailsAdapter warehouseDetailsAdapter;
    private AllocationTubeListAlloDetailsAdapter alloDetailsAdapter;

    private List<AllocationTubeListWareHDetailsBean.DataBean> wareHDetailsList;
    private List<AllocationTubeListAlloDetailsBean.DataBean> alloDetailsList;

    @SuppressLint("HandlerLeak")
    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case GET_ALLOCATION_DETAILS_INFO:
                    alloDetailsAdapter.notifyDataSetChanged();
                    break;
                case GET_WAREHOUSE_DETAILS_INFO:
                    warehouseDetailsAdapter.notifyDataSetChanged();
                    break;

            }
        }
    };
    @Override
    protected void initView() {
        setContentView(R.layout.activity_allocation_tube_list_detail);
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(this);
        Intent intent=getIntent();
        acticityType=intent.getStringExtra("activity_type");
        if(acticityType.equals(ALLOCATION_DETAILS)){
            titleTitle.setText(R.string.goodWarehousing_detail);
            alloDetailsList=new ArrayList<>();
            alloDetailsAdapter=new AllocationTubeListAlloDetailsAdapter(alloDetailsList);
            rv.setLayoutManager(new LinearLayoutManager(this));
            rv.setAdapter(alloDetailsAdapter);
            orderId=intent.getStringExtra("order_id");
            getAllocationInfo();
        }
        else if(acticityType.equals(WAREHOUSE_DETAILS)){
            titleTitle.setText(R.string.WarehousingDetails);
            wareHDetailsList=new ArrayList<>();
            warehouseDetailsAdapter=new AllocationTubeListWareHDetailsAdapter(wareHDetailsList);
            rv.setLayoutManager(new LinearLayoutManager(this));
            rv.setAdapter(warehouseDetailsAdapter);
            warehouseRecordId=intent.getStringExtra("warehouse_record_id");
            getToMarehouseInfo();
        }
    }

    @Override
    protected void initData() {
    }

    @Override
    public void initListener() {

    }

    /* 獲取入庫詳情列表*/
    private void getToMarehouseInfo() {
        loadingDialog.show();
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("warehouse_record_id", warehouseRecordId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        HttpUtils.post(this, Common.ALLOCATION_WAREHOUSING_DETAILS, jsonObject, new TextCallBack() {
            @Override
            protected void onSuccess(String text) {
                AllocationTubeListWareHDetailsBean data= GsonUtils.JsonToBean(text,AllocationTubeListWareHDetailsBean.class);
                if(data.getStatus()==1){
                    wareHDetailsList.addAll(data.getData());
                }else{
                    Toast.makeText(AllocationTubeListDetailsActivity.this,"獲取數據失敗",Toast.LENGTH_SHORT).show();
                }
                loadingDialog.dismiss();
                mHandler.sendEmptyMessage(GET_WAREHOUSE_DETAILS_INFO);
            }

            @Override
            protected void onFailure(ResponseException e) {
                loadingDialog.dismiss();
                Toast.makeText(AllocationTubeListDetailsActivity.this,"獲取數據失敗",Toast.LENGTH_SHORT).show();
            }
        });
    }

    /* 獲取調撥詳情列表*/
    private void getAllocationInfo() {
        loadingDialog.show();
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("id", orderId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        HttpUtils.post(this, Common.ALLOCATION_DETAILS, jsonObject, new TextCallBack() {
            @Override
            protected void onSuccess(String text) {
                AllocationTubeListAlloDetailsBean data= GsonUtils.JsonToBean(text,AllocationTubeListAlloDetailsBean.class);
                if(data.getStatus()==1){
                    alloDetailsList.addAll(data.getData());
                }else{
                    Toast.makeText(AllocationTubeListDetailsActivity.this,"獲取數據失敗",Toast.LENGTH_SHORT).show();
                }
                loadingDialog.dismiss();
                mHandler.sendEmptyMessage(GET_ALLOCATION_DETAILS_INFO);
            }

            @Override
            protected void onFailure(ResponseException e) {
                loadingDialog.dismiss();
                Toast.makeText(AllocationTubeListDetailsActivity.this,"獲取數據失敗",Toast.LENGTH_SHORT).show();
            }
        });
    }


    @OnClick(R.id.title_back)
    public void onViewClicked() {
        finish();
    }
}
